<%-- 
    Document   : test
    Created on : Oct 31, 2010, 9:41:58 AM
    Author     : admin
--%>

<%@page contentType="text/html" import="java.util.regex.*"
        pageEncoding="UTF-8"%>

<%@page import="javax.jcr.Repository,
        org.apache.jackrabbit.j2ee.RepositoryAccessServlet,
        org.apache.jackrabbit.util.Text,
        javax.jcr.Session,
        javax.jcr.SimpleCredentials,
        javax.jcr.query.Query,
        javax.jcr.query.RowIterator,
        java.text.NumberFormat,
        javax.jcr.query.Row,
        javax.jcr.Node,
        java.net.URLEncoder,
        java.text.SimpleDateFormat,
        java.text.DateFormat,
        java.util.List,
        java.util.UUID,
        java.util.ArrayList,
        java.util.Iterator,
        javax.jcr.Value,
        org.apache.jackrabbit.core.version.InternalVersionManager,
        org.apache.jackrabbit.core.version.InternalVersionManagerImpl,
        org.apache.jackrabbit.core.persistence.pool.BundleDbPersistenceManager,
        org.apache.jackrabbit.core.RepositoryImpl,
        org.ilookinto.util.*,
        javax.sql.*,
        java.sql.*,
        javax.jcr.RepositoryException"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<%
    String s = " this is word This THis";
    Pattern p = Pattern.compile("\\bthis is\\b", Pattern.CASE_INSENSITIVE);
    out.println(p.matcher(s).replaceAll("{\\\\highlight7 $0 }"));
    String action = request.getParameter("action");
    String tags = request.getParameter("tags");
    String uuid = request.getParameter("uuid");
    if (tags == null || tags.isEmpty() || uuid == null || uuid.isEmpty()) {
      return;
    }

    Repository rep;
    Session jcrSession;
    try {
      rep = RepositoryAccessServlet.getRepository(pageContext.getServletContext());
      jcrSession = rep.login(new SimpleCredentials("admin", "admin".toCharArray()));

      DataSource ds = (DataSource) session.getAttribute("ds");
      String sql = null;
      if (ds == null) {
        ds = new RepDbUtil().getDataSource(rep);
        session.setAttribute("ds", ds);
      }

      Connection con = ds.getConnection();
      if ("add".equals(action)) {
        String[] tag = tags.split(",");

        long id2 = java.util.UUID.randomUUID().getMostSignificantBits();
        long id3 = java.util.UUID.randomUUID().getMostSignificantBits();
        PreparedStatement pst;
        long bookid = -1;
        pst = con.prepareStatement("SELECT id FROM bookmarks where uuid=?");
        pst.setString(1, uuid);
        ResultSet rs = null;
        rs = pst.executeQuery();
        if (!rs.next()) {
          pst.close();
          sql = " INSERT INTO bookmarks VALUES(?, 'public', ?, 'desc',  ?)";
          pst = con.prepareStatement(sql);
          bookid = java.util.UUID.randomUUID().getMostSignificantBits();
          pst.setLong(1, bookid);
          pst.setString(2, uuid);
          pst.setTimestamp(3, new java.sql.Timestamp(new java.util.Date().getTime()));
          pst.execute();

        } else {
          bookid = rs.getInt(1);
          rs.close();
        }
        pst.close();

        sql = " INSERT INTO bookmarks VALUES(?, 'public', ?, 'desc',  ?)";
        pst = con.prepareStatement(sql);
        //pst.setLong(1, id1);
        pst.setString(2, uuid);
        pst.setTimestamp(3, new java.sql.Timestamp(new java.util.Date().getTime()));
        pst.execute();
        //rs = statement.executeQuery("SELECT * from TAGS");
        /*
        while (rs.next()) {
        out.println(rs.getString(1) + "..." + rs.getString(2));
        }*/
      }
      jcrSession.logout();
    } catch (Throwable e) {
      e.printStackTrace();
      out.println(e);
    }

%>

